package com.credit.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.credit.common.ServerResponse;
import com.credit.mapper.jurisdictionManagementMapper;
import com.credit.mapper.jurisdictionMapper;
import com.credit.mapper.messageListMapper;
import com.credit.mapper.personalQualificationsMapper;
import com.credit.mapper.userMapper;
import com.credit.pojo.jurisdictionManagement;
import com.credit.pojo.personalQualifications;
import com.credit.pojo.user;
import com.credit.service.IUserService;
import com.credit.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements IUserService{

	@Autowired
	private userMapper userMapper;
	
	@Autowired
	private jurisdictionManagementMapper jurisdictionManagementMapper;
	
	@Autowired
	private messageListMapper messageListMapper;
	
	@Autowired
	private jurisdictionMapper jurisdictionMapper;
	
	@Autowired
	private personalQualificationsMapper personalQualificationsMapper;

	
	/**
	 * 用户登录方法
	 */
	public ServerResponse login(user u) {
		u.setStatus(0);
		user user=userMapper.login(u);
		//判断用户名密码
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(201, "用户名密码错误");
		}
		UserVo uVo=new UserVo();
		uVo.setUser(user);
		uVo.setUnread(messageListMapper.selectUnread(user.getId()));     //查看未读信息
		if(user.getCreato()==2) {
			 //为3级权限时候查询用户权限
		     uVo.setJurisdiction(jurisdictionManagementMapper.selectJurisdiction(user.getId()).getJurisdiction()); 
			
			
		}
		
		return ServerResponse.createBySuccess("200", uVo);
	}


	public ServerResponse updateByPrimaryKeySelective(user record) {
		
		return ServerResponse.createBySuccess(userMapper.updateByPrimaryKeySelective(record));
	}


	public ServerResponse insertSelective(user record) {
		record.setStatus(0);
		if(userMapper.login(record) != null) {
			return ServerResponse.createByErrorCodeMessage(201, "账号已经存在");
		}
		
		userMapper.insertSelective(record);
		
		if(record.getCreato()==2) {
			//为3级权限时增加用户权限
			jurisdictionManagement jurisdiction=new jurisdictionManagement();
			jurisdiction.setUserid(record.getId());
			jurisdictionManagementMapper.insertJurisdiction(jurisdiction);
		}
		//为2级权限时增加用户归属
		if(record.getCreato()==1) {
			record.setAffiliation(record.getId());   //增加用户归属为自己id
		}
		return ServerResponse.createBySuccess(userMapper.updateByPrimaryKeySelective(record));
	}


	public ServerResponse updateJurisdiction(jurisdictionManagement record) {
		
		return ServerResponse.createBySuccess(jurisdictionManagementMapper.updateJurisdiction(record));      
	}


	public ServerResponse selectUser(user u,int pageNum,int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		 
		 List<user> list=userMapper.selectUser(u);
		 
		 PageInfo pageInfo = new PageInfo(list);
		
		 return ServerResponse.createBySuccess(pageInfo);
	}


	public ServerResponse selectByPrimaryKey(Integer id) {
		
		return ServerResponse.createBySuccess(userMapper.selectByPrimaryKey(id));
	}


	public ServerResponse selectJurisdictionType() {
		
		return ServerResponse.createBySuccess(jurisdictionMapper.selectJurisdictionType());
	}

	
	
	
	

	public ServerResponse deletePersonalQualifications(Integer id) {
		
		return ServerResponse.createBySuccess(personalQualificationsMapper.deletePersonalQualifications(id));
	}


	public ServerResponse insertPersonalQualifications(personalQualifications record) {
		
		return ServerResponse.createBySuccess(personalQualificationsMapper.insertPersonalQualifications(record));
	}


	public ServerResponse selectPersonalQualifications(Integer id) {
		
		return ServerResponse.createBySuccess(personalQualificationsMapper.selectPersonalQualifications(id));
	}


	public ServerResponse updatePersonalQualifications(personalQualifications record) {
		
		return ServerResponse.createBySuccess(personalQualificationsMapper.updatePersonalQualifications(record));
	}



	
	
	

	

	
}
